/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GoLib-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_01_01			*/
/*	File:		GoHash.go			*/
/********************************************************/

package GoCap

const (
	GO_HS_PLUS	uint64 = 0X00003039
	GO_HS_MASK	uint64 = 0X7FFFFFFF
	GO_HS_BASE	uint64 = 0X238F13AF
	GO_HS_OSET	uint64 = 0X41C64E6B
)

var GoBkt = []uint64{
	5,           7,           53,           97,           193,
	389,         769,         1543,         3079,         6151,
	12289,       24593,       49157,        98317,        196613,
	393241,      786433,      1572869,      3145739,      6291469,
	12582917,    25165843,    50331653,     100663319,    201326611,
	402653189,   805306457,   1610612741,   3221225473,   4294967291,
}

type GoHash struct {
	bkt	uint64
	ptr	[]interface{}
}

func (h *GoHash) GoHashInit(bkt uint64) {
	h.ptr = make([]interface{}, bkt)
	h.bkt = bkt
}

func (h *GoHash) GoHashBkt() (bkt uint64) {
	return h.bkt
}

func (h *GoHash) GoHashInto(bkt uint64, val interface{}) {
	h.ptr[bkt % h.bkt] = val
}

func (h *GoHash) GoHashFrom(bkt uint64) (val interface{}) {
	return h.ptr[bkt % h.bkt]
}

func (h *GoHash) GoHashFunc(str string) (bkt uint64) {
	bkt = GO_HS_BASE * uint64(len(str))
	for i, b := range []byte(str) {
		bkt = (bkt + (uint64(b) << (uint64(i) * 13 % 24))) & GO_HS_MASK
	}
	return (bkt * GO_HS_OSET + GO_HS_PLUS) & GO_HS_MASK;
}
